package com.drerp.RH.DAO;

import java.util.Calendar;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

import com.drerp.RH.Cargo;
import com.drerp.RH.Colaborador;
import com.drerp.RH.HorasTrabalho;

public class ColaboradorDAO extends GenericoDAO<Colaborador, Long> {
	
	public List<Colaborador> encontrarColaboresAtivos() {
		List<Colaborador> colaboradores = null;
		
		Transaction trans = null;
		Session session = super.getSession();
		
		try {
			trans = session.beginTransaction();
			
			String hql = "SELECT c FROM Colaborador c WHERE c.ativo = true ";
			Query query = session.createQuery(hql);
			colaboradores = super.findMany(query);
			
			trans.commit();
		} catch (RuntimeException e) {
			if (trans != null) {
				trans.rollback();
			}

			e.printStackTrace();
		}
		
		return colaboradores;
	}
	
	public List<Colaborador> encontrarColaboresAfastados() {
		List<Colaborador> colaboradores = null;
		
		Transaction trans = null;
		Session session = super.getSession();
		
		try {
			trans = session.beginTransaction();
			
			String hql = "SELECT c FROM Colaborador c WHERE c.ativo = false";
			Query query = session.createQuery(hql);
			colaboradores = super.findMany(query);
			
			trans.commit();
		} catch (RuntimeException e) {
			if (trans != null) {
				trans.rollback();
			}

			e.printStackTrace();
		}
		
		return colaboradores;
	}

	public Colaborador encontrarColaboradorPorNome(String nome) {
		Colaborador colaborador = null;
		
		Transaction trans = null;
		Session session = super.getSession();
		
		try {
			trans = session.beginTransaction();
			
			String hql = "SELECT c FROM Colaborador c WHERE c.nome like :nome";
			Query query = session.createQuery(hql).setParameter("nome", nome + "%");
			colaborador = super.findOne(query);
			
			trans.commit();
		} catch (RuntimeException e) {
			if (trans != null) {
				trans.rollback();
			}

			e.printStackTrace();
		}
		
		return colaborador;
	}
	
	public List<Colaborador> encontrarColaboresPorCargo(Cargo cargo) {
		List<Colaborador> colaboradores = null;
		
		Transaction trans = null;
		Session session = super.getSession();
		
		try {
			trans = session.beginTransaction();
			
			String hql = "SELECT c FROM Colaborador c WHERE c.cargo = :cargo ";
			Query query = session.createQuery(hql).setParameter("cargo", cargo);
			colaboradores = super.findMany(query);
			
			trans.commit();
		} catch (RuntimeException e) {
			if (trans != null) {
				trans.rollback();
			}

			e.printStackTrace();
		}
		
		return colaboradores;
	}

	public long contarColaboradoresPorCargo(Cargo cargo) {
		long numero = -1;
		
		Transaction trans = null;
		Session session = super.getSession();
		
		try {
			trans = session.beginTransaction();
			
			String hql = "SELECT count(*) FROM Colaborador c WHERE c.cargo = :cargo ";
			Query query = session.createQuery(hql).setParameter("cargo", cargo);
			numero = (Long) query.uniqueResult();
			
			trans.commit();
		} catch (RuntimeException e) {
			if (trans != null) {
				trans.rollback();
			}

			e.printStackTrace();
		}
		
		return numero;
	}

	public List<HorasTrabalho> encontrarHorasTrabalhadasPorColaborador(Colaborador colaborador) {
		List<HorasTrabalho> horas = null;
		
		Transaction trans = null;
		Session session = super.getSession();
		
		try {
			trans = session.beginTransaction();
			
			String hql = "SELECT h FROM BancoDeHoras h WHERE h.colaborador = :colaborador";
			Query query = session.createQuery(hql).setParameter("colaborador", colaborador);
			
			HorasTrabalhoDAO horasDAO = new HorasTrabalhoDAO();
			horas = horasDAO.findMany(query);
			
			trans.commit();
		} catch (RuntimeException e) {
			if (trans != null) {
				trans.rollback();
			}

			e.printStackTrace();
		}
		
		return horas;
	}
	
	public List<HorasTrabalho> encontrarHorasTrabalhadasPorColaboradorEntreDatas(Colaborador colaborador, Calendar inicio, Calendar fim) {
		List<HorasTrabalho> horas = null;
		
		Transaction trans = null;
		Session session = super.getSession();
		
		try {
			trans = session.beginTransaction();
			
			String hql = "SELECT h FROM BancoDeHoras h WHERE h.colaborador = :colaborador AND h.dataEntrada > :inicioMes AND h.dataSaida < :fimMes";
			Query query = session.createQuery(hql).setParameter("colaborador", colaborador).setParameter("inicioMes", inicio).setParameter("fimMes", fim);
			
			HorasTrabalhoDAO horasDAO = new HorasTrabalhoDAO();
			horas = horasDAO.findMany(query);
			
			trans.commit();
		} catch (RuntimeException e) {
			if (trans != null) {
				trans.rollback();
			}

			e.printStackTrace();
		}
		
		return horas;
	}
}
